<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        html,body{
            width: 100%;
            height: 100%;
        }
        .top{
            text-align: center;
            margin-bottom: 20px;
        }
        .top input{
            height: 30px;
        }
        .center{
            height: 30px;
            display: flex;
            justify-content: space-around;
            background-color: lightcoral;
            margin-bottom: 20px;
            line-height: 30px;
        }
        .active{
            background-color: lightgreen;
        }
        .button{
            color: #00C9BD;
            background-color: white;
            border: 0;
        }
        .empty{
            color: red;
            font-size: 14px;
            margin: 102px 60px;
        }
        .middle{
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px;
        }
        .middle div{
            width: 120px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            border-radius: 20px;
            background-color: lightskyblue;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="top">
            <input type="text" v-model="inputText" placeholder="输入任务名称">
        <input type="button" value="新增" @click="add()" class="button">
        </div>
        <div class="middle">
            <div>全部任务（{{zongshu}}）</div>
            <div>已完成{{finished}}</div>
            <div style="background-color: lightcoral;">未完成（{{Unfinished}}）</div>
        </div>
        <div :class="`center ${item.status? 'active':''}`" v-for="(item,index) in datalist" :key="index">
                <input type="checkbox" @click="changeStatus(item)">
                <p>编号:{{item.id}}</p>
                <p>{{item.name}}</p>
                <button @click="shanchu(index)">删除</button>
        </div>
        <div class="empty" v-if="datalist.length==0">任务列表为空,请添加一个任务！</div>
    </div>
</body>
</html>
<script src="./vue-2.5.21.js"></script>
<script>
    var v=new Vue({
        el:'#app',
        data:{
            wancheng:0,
            inputText:'',
            datalist:[
                // {id:1,name:'任务名称',status:true}
            ]
        },
        computed:{
            zongshu(){
                return this.datalist.length
            },
            finished(){
               return this.datalist.filter((item)=>{return item.status}).length
            },
            Unfinished(){
                return this.datalist.length-this.datalist.filter((item)=>{return item.status}).length

            }
        },
        methods:{
            add(){
                var obj={id:this.datalist.length+1,name:this.inputText,status:false}
                //输入框不为空时，才去添加数据
               if(this.inputText){
                this.datalist.push(obj)
                this.inputText=''
               }
                
            },
            shanchu(index){
                console.log(index);
                this.datalist.splice( index,1)
            },
            changeStatus(item){
                item.status=!item.status
                this.wancheng++
            }
        }
    })
</script>